Cocoapods 私有仓库

Cocoapods仓库分为两类:公共仓库、私有仓库

公共仓库:项目中用pod管理的第三方库,我们也可以创建自己的pod仓库。

私有仓库:主要用于自己一些公共库封装管理;或者项目组件化后,各个组件独立项目,成为一个pod仓库

本文主要讨论Cocoapods私有仓库的创建:

私有索引库

创建私有索引仓库

这里用的是百度云 coding.net(其他推荐gitee)
注意Github 创建私有仓库需要付费

image-20190311150441424

添加本地pod索引

1
$ pod repo add ZWKSpecs https://e.coding.net/jindouyun_wk/ZWKSpecs.git

查看本地索引库

1
2
3
4
5
6
7
8
9
10
11
$ pod repo

master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/dianshi/.cocoapods/repos/master

ZWKSpecs
- Type: git (master)
- URL: https://e.coding.net/jindouyun_wk/ZWKSpecs.git
- Path: /Users/dianshi/.cocoapods/repos/ZWKSpecs

创建组件库

首先创建组件仓库 ZWKExtensions
-w597

快速创建模版库

1
pod lib create ZWKExtensions

cd 进入项目,初始化创建podspec文件

1
pod spec create ZWKCategory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
What is your name?
> janicezhw

! Setting your name in git to janicezhw
git config user.name "janicezhw"

What is your email?
> janicezhw@gmail.com

! Setting your email in git to janicezhw@gmail.com
git config user.email "janicezhw@gmail.com"

------------------------------

What platform do you want to use?? [ iOS / macOS ]
> iOS

What language do you want to use?? [ Swift / ObjC ]
> Swift

Would you like to include a demo application with your library? [ Yes / No ]
> Yes

Which testing frameworks will you use? [ Quick / None ]
> None

Would you like to do view based testing? [ Yes / No ]
> No

会自动创建项目并执行 pod install
并打开workspace
class 文档里面就是放pod的内容
-w616

添加组件内容

把组件库项目代码文件引入,注意选择 Copy item & Add to targets
15844368003572

安装与测试本地库

可以在项目中引入库并进行相应测试
注意组件库中的方法需要是 public 才能在整个工程调用

1
2
3
4
5
6
7
8
public var borderColor: UIColor {
get {
UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue.cgColor
}
}

import 库名称

1
2
3
4
5
6
7
import ZWKExtensions

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.borderColor = .red
}

修改spec配置文件

在.podspec 文件中进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Pod::Spec.new do |s|
s.name = 'ZWKExtensions'
s.version = '0.1.0'
s.summary = 'some Extensions with system Lib.'

# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!

s.description = <<-DESC
TODO: Add long description of the pod here.
DESC

s.homepage = 'https://jindouyun_wk.coding.net/p/ZWKExtensions/'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'janicezhw' => 'janicezhw@gmail.com' }
s.source = { :git => 'https://e.coding.net/jindouyun_wk/ZWKExtensions.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

s.source_files = 'ZWKExtensions/Classes/**/*'

s.swift_versions = '4.0' # 注意如果是swift版本,需要添加这个设置

# s.resource_bundles = {
# 'ZWKExtensions' => ['ZWKExtensions/Assets/*.png']
# }

# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end

上传组件库代码

先进行本地验证

pod lib lint 如果是私有库 建议 pod lib lint --private

将代码提交到组件仓库

1
2
3
4
5
6
git add .
git commit -m 'firstCommit'
git remote add origin https://e.coding.net/jindouyun_wk/ZWKExtensions.git
// 第一次push如果报错的话可以加上-f
// git push -f origin master
git push origin master

打标签

1
2
git tag '0.1.0'  # 注意需要与 .podspec 中配置的版本号一致
git push --tags # 推送tag

提交组件库到索引库

远程验证

1
2
// 远程验证会验证 s.source 中的tag,如果此时没有打上相应的标签则会报错
pod spec lint

提交podspec

1
2
// pod repo push 私有索引库名称 spec名称.podspec 
pod repo push ZWKSpecs ZWKExtensions.podspec

完成后更新pod索引
pod repo update ZWKSpecs

使用私有库

添加 Podfile 文件

1
pod init

配置Podfile

顶部填写 pod source

1
2
3
4
source 'https://e.coding.net/jindouyun_wk/ZWKExtensions.git'
source 'https://github.com/CocoaPods/Specs.git'

pod 'LXFBase'

安装及使用

1
pod install

完成后

1
import ZWKExtensions

参考文章
https://juejin.im/post/5ac5d5abf265da2396129e63
https://swiftcafe.io/post/cocoapods-private

关于版本更新,依赖,子库问题:
https://juejin.im/post/5ac5daf451882555627d8491